package com.qiku.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qiku.dao.ResourceMapper;
import com.qiku.domin.Resource;
import com.qiku.domin.ResourceCategory;
import com.qiku.domin.ResourceVO;
import com.qiku.service.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ResourceServiceImpl implements ResourceService {

    @Autowired
    private ResourceMapper resourceMapper;

    @Override
    public List<ResourceCategory> findAllResourceCategory() {
        List<ResourceCategory> categoryList = resourceMapper.findAllResourceCategory();
        return categoryList;
    }

    @Override
    public PageInfo<Resource> findAllResource(ResourceVO resourceVO) {
        PageHelper.startPage(resourceVO.getCurrentPage() , resourceVO.getPageSize());
        List<Resource> resourceList = resourceMapper.findAllResource(resourceVO);
        PageInfo<Resource> pageInfo = new PageInfo<>(resourceList);
        return pageInfo;
    }

    @Override
    public void saveResource(Resource resource) {
        resource.setCreatedTime(new Date());
        resource.setUpdatedTime(new Date());
        resourceMapper.saveResource(resource);
    }

    @Override
    public void UpdateResource(Resource resource) {
        resource.setUpdatedTime(new Date());
        resourceMapper.UpdateResource(resource);
    }

    @Override
    public void deleteResource(Integer id) {
        resourceMapper.deleteRoleAndResource(id);
        resourceMapper.deleteResource(id);
    }
}
